<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>放大镜</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        font-size: 0;
      }
      div {
        display: inline-block;
      }
      .min {
        width: 300px;
        height: 300px;
        position: relative;
      }
      .mink {
        width: 100px;
        height: 100px;
        display: none;
        position: absolute;
        top: 0;
        left: 0;
        background-color: rgba(0, 0, 0, 0.3);
      }
      .max {
        width: 300px;
        height: 300px;
        position: relative;
        overflow: hidden;
      }
      .maxk {
        width: 900px;
        height: 900px;
        position: absolute;
        top: 0;
        left: 0;
      }
    </style>
  </head>
  <body>
    <div class="min" id="min">
      <img class="min" id="minimg" src="./AOS2CON.png" alt="" />
      <div class="mink" id="mink"></div>
    </div>
    <div class="max" id="max">
      <img class="maxk" id="maxk" src="./AOS2CON.png" alt="" />
    </div>
  </body>
  <script>
    var min = document.getElementById("min"),
      mink = document.getElementById("mink"),
      max = document.getElementById("max"),
      maxk = document.getElementById("maxk");
    min.onmouseenter = function () {
      mink.style.display = "block";
      min.onmousemove = function (e) {
        var x, y;
        var sx = e.clientX - min.offsetLeft;
        var sy = e.clientY - min.offsetTop;
        x = sx;
        y = sy;
        if (sx > min.clientWidth - mink.clientWidth / 2) {
          x = min.clientWidth - mink.clientWidth / 2;
          console.log("检测", sx);
        } else if (sx < mink.clientWidth / 2) {
          x = mink.clientWidth / 2;
        }

        if (sy > min.clientHeight - mink.clientHeight / 2) {
          y = min.clientHeight - mink.clientHeight / 2;
        } else if (sy < mink.clientHeight / 2) {
          y = mink.clientHeight / 2;
        }

        x = x - mink.clientWidth / 2;
        y = y - mink.clientHeight / 2;

        mink.style.left = x + "px";
        mink.style.top = y + "px";

        console.log(x, y);

        maxk.style.left = -x * (maxk.clientWidth / min.clientWidth) + "px";
        maxk.style.top = -y * (maxk.clientHeight / min.clientHeight) + "px";
        console.log(x, y);
      };
    };
  </script>
</html>
